﻿using Common.Contract;
using Common.Contract.Customer;
using Common.Contract.Customer.Request;
using Common.Contract.SaleOut;
using Common.Contract.SaleOut.Request;
using Common.Contract.Warranty.Request;
using Common.Service;
using Common.Service.Command;
using Common.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warranty.Configuration;
using Warranty.Model.Interface;
using Warranty.Model.Model;

namespace Warranty.ServiceCommand
{
    public class UpdateWarrantyMessageServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateWarrantyMessageRequest, ResponseRecord>
    {
        public UpdateWarrantyMessageServiceCommand(IProviderFactory provider) : base(provider) { }

        protected override void Validate(UpdateWarrantyMessageRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.Record;
            var obj = new WarrantyMessage()
            {
                ConfirmUser = record.ConfirmUser,
                EndDate = record.EndDate,
                EndUserCell = record.EndUserCell,
                LastUpdateDate = DateTime.Now,
                Model = record.Model,
                RespMsg = record.ResponseMessage,
                RespType = record.ResponseType,
                SellinDate = record.SellinDate,
                SelloutDate = record.SelloutDate,
                SerialNo = record.SerialNumber,
                SelloutTime = record.SelloutTime,
                SmsYn = record.SmsYn.HasValue ? record.SmsYn.Value.ToBooleanString() : null,
                StartDate = record.StartDate,
                Suffix = record.Suffix,
                TransferFlag = record.TransferFlag.HasValue ? record.TransferFlag.Value.ToBooleanString() : null,
            };
            return Providers.WarrantyMessage.Update(obj);
        }
    }
}
